# -*- coding: utf-8 -*-
import re

line_seperator = "\r\n"
p_file = re.compile("--- ([^\t]+)\t.*?" + line_seperator + "\+\+\+ ([^\t]+)\t.*?" + line_seperator)
p_change = re.compile("@@ -(\d+),(\d+) \+(\d+),(\d+) @@")
file_seperator = line_seperator + "(?=---)"
chunk_seperator = line_seperator + "(?=@@ -)"

def set_line_seperator(seperator):
	global line_seperator, p_file, p_change, file_seperator, chunk_seperator
	line_seperator = seperator
	p_file = re.compile("--- ([^\t]+)\t.*?" + line_seperator + "\+\+\+ ([^\t]+)\t.*?" + line_seperator)
	p_change = re.compile("@@ -(\d+),(\d+) \+(\d+),(\d+) @@")
	file_seperator = line_seperator + "(?=---)"
	chunk_seperator = line_seperator + "(?=@@ -)"

def get_line_increment(chunk):
#	print "get_line_increment:%r" % chunk
	ret = set()
	lines = chunk.split(line_seperator);
#	print lines
	rmo = p_change.match(lines[0]);
	if rmo == None:
		return ret
#	print "match success"
	old_begin, old_len, new_begin, new_len = map(int, rmo.group(1,2,3,4))
	line_old, line_new = old_begin, new_begin
	for mline in lines[1:]:
		if line_new - new_begin >= new_len:
			break;
		if mline[0] == ' ':
			line_old += 1
			line_new += 1
		elif mline[0] == '+':
#			print "line_new: ",line_new
			ret.add(line_new)
			line_new += 1
		elif mline[0] == '-':
			line_old += 1
		elif mline[0] == '\\':
			None
		else:
			print "Something wrong happend~"
	return ret

def get_file_increment(fileblock):
#	print "get_file_increment:%r" % fileblock
	rmo = p_file.match(fileblock)
	if rmo == None:
		return None,None
	key = rmo.group(2)
	val = set()
	chunks = re.split(chunk_seperator, fileblock)
	for mchunk in chunks:
		val |= get_line_increment(mchunk)
#		print "val",val
	return key,val


def get_increment(difftext):
#	print "%r" % line_seperator
#	print "get_line_increment:%r" % difftext
	ret = {}
	files = re.split(file_seperator, difftext)
	for mfile in files:
		k,v = get_file_increment(mfile)
		if v and len(v) > 0:
			ret[k] = v
	return ret



def test():
	testfile="""--- /path/to/original	''timestamp''
+++ /path/to/new	''timestamp''
@@ -1,3 +1,9 @@
+This is an important
+notice! It should
+therefore be located at
+the beginning of this
+document!
+
 This part of the
 document has stayed the
 same from version to
@@ -5,16 +11,10 @@
 be shown if it doesn't
 change.  Otherwise, that
 would not be helping to
-compress the size of the
-changes.
-
-This paragraph contains
-text that is outdated.
-It will be deleted in the
-near future.
+compress anything.
 
 It is important to spell
-check this dokument. On
+check this document. On
 the other hand, a
 misspelled word isn't
 the end of the world.
@@ -22,3 +22,7 @@
 this paragraph needs to
 be changed. Things can
 be added after it.
+
+This paragraph contains
+important new additions
+to this document.
--- /path/to/ASDAD	''timestamp''
+++ /path/to/ASDAD	''timestamp''
@@ -1,3 +1,9 @@
+This is an important
+notice! It should
+therefore be located at
+the beginning of this
+document!
+
 This part of the
 document has stayed the
 same from version to
@@ -5,16 +11,10 @@
 be shown if it doesn't
 change.  Otherwise, that
 would not be helping to
-compress the size of the
-changes.
-
-This paragraph contains
-text that is outdated.
-It will be deleted in the
-near future.
+compress anything.
 
 It is important to spell
-check this dokument. On
+check this document. On
 the other hand, a
 misspelled word isn't
 the end of the world.
@@ -22,3 +22,7 @@
 this paragraph needs to
 be changed. Things can
 be added after it.
+
+This paragraph contains
+important new additions
+to this document."""
	set_line_seperator("\n")
	print get_increment(testfile)

if __name__ == '__main__':
	test()